﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using MyFeer.Widget.Grid.Cell;
using System.Windows.Input;
using System.Windows.Shapes;

namespace MyFeer.Widget.Grid
{
    public class RowHeader
    {
        private GridViewer gridViewer;
        private bool isPresssMouseLeftButton = false;
        private Point pressPos;
        private int moveRowIndex;
        private bool canMove = false;
        private Line brokenLine = new Line();
        public RowHeader(GridViewer gridViewer)
        {
            this.gridViewer = gridViewer;
            this.gridViewer.MouseMove += new System.Windows.Input.MouseEventHandler(GridViewer_MouseMove);
            this.gridViewer.MouseLeftButtonDown += new MouseButtonEventHandler(GridViewer_MouseLeftButtonDown);
            this.gridViewer.MouseLeftButtonUp += new MouseButtonEventHandler(gridViewer_MouseLeftButtonUp);
            DoubleCollection doubleCollection=new DoubleCollection();
            doubleCollection.Add(2);
            doubleCollection.Add(4);
            doubleCollection.Add(3);
            doubleCollection.Add(2);
            this.brokenLine.StrokeDashArray = doubleCollection;
            this.brokenLine.Stroke = new SolidColorBrush(Colors.Gray);
            this.brokenLine.StrokeThickness = 0.5;
        }

        void gridViewer_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            Point mousePos = e.GetPosition(this.gridViewer.GridCanvas);
            isPresssMouseLeftButton = false;
            if (this.gridViewer.GridCanvas.Children.Contains(this.brokenLine))
            {
                this.gridViewer.GridCanvas.Children.Remove(this.brokenLine);
            }
            if (this.canMove)
            {
                int rowHeight = this.gridViewer.GridModel.getRowHeight(this.moveRowIndex);
                rowHeight = (int)(mousePos.Y - pressPos.Y + rowHeight);
                this.gridViewer.GridModel.setRowHeight(this.moveRowIndex, rowHeight);
                this.gridViewer.Refersh();
            }
            this.canMove = false;
        }

        void GridViewer_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Point mousePos = e.GetPosition(this.gridViewer.GridCanvas);
            if (mousePos.X > GridConst.DefaultRowHeaderWidth)
            {
                return;
            }
            pressPos=e.GetPosition(this.gridViewer.GridCanvas);
            isPresssMouseLeftButton = true;
            //if (this.canMove)
            //{
                int rowHeight = this.gridViewer.GridModel.getRowHeight(this.moveRowIndex);
                double linePosY = mousePos.Y;
                this.brokenLine.X1 = 0;
                this.brokenLine.X2 = this.gridViewer.GridCanvas.RenderSize.Width;
                this.brokenLine.Y1 = 0;
                this.brokenLine.Y2 = 0;
                this.brokenLine.Width = this.gridViewer.GridCanvas.RenderSize.Width;
                this.brokenLine.Height = 2;
                Canvas.SetLeft(this.brokenLine, 0);
                Canvas.SetTop(this.brokenLine, linePosY);
                this.gridViewer.GridCanvas.Children.Add(this.brokenLine);
            //}
        }

        void GridViewer_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            gridViewer.Cursor = Cursors.Arrow;
            Point mousePos=e.GetPosition(this.gridViewer.GridCanvas);
            if (mousePos.X > GridConst.DefaultRowHeaderWidth)
            {
                return;
            }
            if (this.isPresssMouseLeftButton && this.canMove)
            {
                gridViewer.Cursor = Cursors.SizeWE;
                int rowHeight=this.gridViewer.GridModel.getRowHeight(this.moveRowIndex);
                double linePosY = mousePos.Y;
                if (pressPos.Y - mousePos.Y > (rowHeight - 2))
                {
                    linePosY = pressPos.Y - rowHeight + 2;
                }
                Canvas.SetLeft(this.brokenLine, 0);
                Canvas.SetTop(this.brokenLine, linePosY);
            }
            else
            {
                
                List<int> visibleRowIndexs = this.gridViewer.VisibleRowIndexs;
                int rowPosY = 0;
                for (int i = 0; i < visibleRowIndexs.Count; i++)
                {
                    int rowIndex = visibleRowIndexs[i];
                    int rowHeight = this.gridViewer.GridModel.getRowHeight(rowIndex);
                    rowPosY += rowHeight;
                    if (Math.Abs(rowPosY - (mousePos.Y - GridConst.DefaultColumnHeaderHeight)) < 2)
                    {
                        gridViewer.Cursor = Cursors.SizeNS;
                        moveRowIndex = rowIndex;
                        canMove = true;
                    }
                }
            }
            
        }

        public void render(int rowIndex, int rowPos, int rowHeight)
        {
            Canvas mainCanvas=this.gridViewer.MainCanvas;
            CellModel cellModel = new CellModel();
            cellModel.CellStyle = GridConst.HeaderCellStyle;
            CellItem cellItem = new CellItem(this.gridViewer, cellModel);
            cellItem.render(0, rowPos, GridConst.DefaultRowHeaderWidth, rowHeight);
            SolidColorBrush scb = new SolidColorBrush(Colors.Black);
            TextBlock textBlock = new TextBlock()
            {
                Height = rowHeight,
                Width = GridConst.DefaultRowHeaderWidth,
                Text = ""+(rowIndex+1),
                TextAlignment=TextAlignment.Center
            };
            Canvas.SetTop(textBlock, rowPos);
            Canvas.SetLeft(textBlock, 0);
            mainCanvas.Children.Add(textBlock);
        }
    }
}
